package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewayStartupLog;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayStartupLogMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayStartupLogParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayStartupLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 网关启动日志 服务实现类
 *
 * @author ssy
 * @date 2022/09/16 10:13
 */
@Service
public class BizGatewayStartupLogServiceImpl extends ServiceImpl<BizGatewayStartupLogMapper, BizGatewayStartupLog> implements BizGatewayStartupLogService {

    /**
     * 分页查询网关启动日志
     *
     * @param bizGatewayStartupLogParam
     * @return
     */
    @Override
    public PageResult<BizGatewayStartupLog> page(BizGatewayStartupLogParam bizGatewayStartupLogParam) {
        // 构造条件
        LambdaQueryWrapper<BizGatewayStartupLog> queryWrapper = new LambdaQueryWrapper<>();
        if (bizGatewayStartupLogParam != null && ObjectUtil.isNotEmpty(bizGatewayStartupLogParam.getGatewayCode())) {
            queryWrapper.eq(BizGatewayStartupLog::getGatewayCode, bizGatewayStartupLogParam.getGatewayCode());
        } else {
            queryWrapper.eq(BizGatewayStartupLog::getGatewayCode, "-1");
        }
        if (bizGatewayStartupLogParam != null && ObjectUtil.isNotEmpty(bizGatewayStartupLogParam.getGatewayCodes())) {
            final String[] gatewayCodes = bizGatewayStartupLogParam.getGatewayCodes().split(",");
            if (gatewayCodes.length > 0) {
                queryWrapper.or().in(BizGatewayStartupLog::getGatewayCode, gatewayCodes);
            }
        }
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    /**
     * 添加网关启动日志
     *
     * @param bizGatewayStartupLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizGatewayStartupLogParam bizGatewayStartupLogParam) {
        // 将dto转为实体
        BizGatewayStartupLog bizGatewayStartupLog = new BizGatewayStartupLog();
        BeanUtil.copyProperties(bizGatewayStartupLogParam, bizGatewayStartupLog);
        this.save(bizGatewayStartupLog);
    }

    /**
     * 删除网关启动日志
     *
     * @param bizGatewayStartupLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayStartupLogParam bizGatewayStartupLogParam) {
        this.removeById(bizGatewayStartupLogParam.getId());
    }

    /**
     * 编辑网关启动日志
     *
     * @param bizGatewayStartupLogParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizGatewayStartupLogParam bizGatewayStartupLogParam) {
        // 根据id查询实体
        BizGatewayStartupLog bizGatewayStartupLog = this.queryBizGatewayStartupLog(bizGatewayStartupLogParam);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayStartupLogParam, bizGatewayStartupLog);
        this.updateById(bizGatewayStartupLog);
    }

    /**
     * 查看详情网关启动日志
     *
     * @param bizGatewayStartupLogParam
     * @return
     */
    @Override
    public BizGatewayStartupLog detail(BizGatewayStartupLogParam bizGatewayStartupLogParam) {
        return this.queryBizGatewayStartupLog(bizGatewayStartupLogParam);
    }


    /**
     * 获取网关启动日志
     *
     * @author ssy
     * @since 2022/09/16 10:13
     */
    private BizGatewayStartupLog queryBizGatewayStartupLog(BizGatewayStartupLogParam bizGatewayStartupLogParam) {
        return this.getById(bizGatewayStartupLogParam.getId());
    }
}
